package controllers

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"

	"test.com/app/headlers"
	"test.com/app/models"
	"test.com/database"
)

// UserIndex 获取单个用户
func UserIndex(c *gin.Context) {
	// 获取user/:id 中的id 并转成uint
	userID, err := strconv.Atoi(c.Param("id"))
	headlers.CheckErr(err)
	user := models.User{ID: uint(userID)}
	// 查找user
	database.GetEloquent().First(&user)
	// 返回json
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "成功",
		"data":    user,
	})
}
